﻿using Common;
using DBUtility;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace Business
{
    public class PAY_TYPE
    {
        #region http上下文
        public static HttpContext Current
        {
            get { return HttpContext.Current; }
        }

        public static HttpRequest Request
        {
            get { return Current.Request; }
        }

        public static HttpResponse Response
        {
            get { return Current.Response; }
        }
        #endregion
        #region model
        public int ID { get; set; }
        /// <summary>
        /// 支付名称
        /// </summary>
        public string title { get; set; }
        /// <summary>
        /// 目录名
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 商户号
        /// </summary>
        public string mch_id { get; set; }
        /// <summary>
        /// 微信APPSECRET
        /// </summary>
        public string app_secret { get; set; }
        /// <summary>
        /// 微信APPID
        /// </summary>
        public string app_id { get; set; }
        /// <summary>
        /// 商户密钥
        /// </summary>
        public string api_key { get; set; }
        /// <summary>
        /// 微信支付类型（0公众号、1二维码）
        /// </summary>
        public int wx_type { get; set; }
        /// <summary>
        /// 智付 密钥
        /// </summary>
        public string private_key { get; set; }
        /// <summary>
        /// 智付 公钥
        /// </summary>
        public string public_key { get; set; }
        /// <summary>
        /// 智付 绑定域名
        /// </summary>
        public string web_url { get; set; }
        /// <summary>
        /// 证书文件
        /// </summary>
        public string file { get; set; } 
        #endregion

        #region Function
        public PAY_TYPE() { }
        public PAY_TYPE Get(int ID)
        {
            PAY_TYPE PAY_TYPE = null;
            MySqlDataReader sdr = null;
            using (sdr = MYSQL.ExecuteReader(string.Format("select * From PAY_TYPE where ID={0}", ID)))
            {
                if (sdr.Read())
                {
                    PAY_TYPE = GetData(PAY_TYPE, sdr);
                }
            }
            sdr.Close();
            return PAY_TYPE;
        }
        public PAY_TYPE SelectByInt(String where, int value)
        {
            PAY_TYPE PAY_TYPE = null;
            MySqlDataReader sdr = null;
            using (sdr = MYSQL.ExecuteReader(string.Format("select * From PAY_TYPE where {0}={1} ", where, value)))
            {
                while (sdr.Read())
                {
                    PAY_TYPE = GetData(PAY_TYPE, sdr);
                }
            }
            sdr.Close();
            return PAY_TYPE;
        }
        public PAY_TYPE SelectByStr(String where, String value)
        {
            PAY_TYPE PAY_TYPE = null;
            MySqlDataReader sdr = null;
            using (sdr = MYSQL.ExecuteReader(string.Format("select * From PAY_TYPE where {0}='{1}' ", where, value)))
            {
                while (sdr.Read())
                {
                    PAY_TYPE = GetData(PAY_TYPE, sdr);
                }
            }
            sdr.Close();
            return PAY_TYPE;
        }
        public PAY_TYPE SelectByObj(String where)
        {
            PAY_TYPE PAY_TYPE = null;
            MySqlDataReader sdr = null;
            using (sdr = MYSQL.ExecuteReader(string.Format("select * From PAY_TYPE where 1=1 and {0} ", where)))
            {
                while (sdr.Read())
                {
                    PAY_TYPE = GetData(PAY_TYPE, sdr);
                }
            }
            sdr.Close();
            return PAY_TYPE;
        }
        public List<PAY_TYPE> GetALL()
        {
            PAY_TYPE PAY_TYPE = null;
            MySqlDataReader sdr = null;
            List<PAY_TYPE> list = new List<PAY_TYPE>();
            using (sdr = MYSQL.ExecuteReader("select * From PAY_TYPE"))
            {
                while (sdr.Read())
                {
                    PAY_TYPE = GetData(PAY_TYPE, sdr);
                    list.Add(PAY_TYPE);
                }
            }
            sdr.Close();
            return list;
        }
        public List<PAY_TYPE> GetALL(String where)
        {
            PAY_TYPE PAY_TYPE = null;
            MySqlDataReader sdr = null;
            List<PAY_TYPE> list = new List<PAY_TYPE>();
            using (sdr = MYSQL.ExecuteReader(string.Format("select * From PAY_TYPE where 1=1 and {0}", where)))
            {
                while (sdr.Read())
                {
                    PAY_TYPE = GetData(PAY_TYPE, sdr);
                    list.Add(PAY_TYPE);
                }
            }
            sdr.Close();
            return list;
        }
        /// <summary>
        /// 获取前几个
        /// </summary>
        /// <param name="where"></param>
        /// <param name="top"></param>
        /// <returns></returns>
        public List<PAY_TYPE> GetALL(String where, int top)
        {
            PAY_TYPE PAY_TYPE = null;
            MySqlDataReader sdr = null;
            List<PAY_TYPE> list = new List<PAY_TYPE>();
            using (sdr = MYSQL.ExecuteReader(string.Format("select top " + top + " * From PAY_TYPE where 1=1 and {0}", where)))
            {
                while (sdr.Read())
                {
                    PAY_TYPE = GetData(PAY_TYPE, sdr);
                    list.Add(PAY_TYPE);
                }
            }
            sdr.Close();
            return list;
        }
        public List<PAY_TYPE> GetList(String where)
        {
            PAY_TYPE PAY_TYPE = null;
            MySqlDataReader sdr = null;
            List<PAY_TYPE> list = new List<PAY_TYPE>();
            using (sdr = MYSQL.ExecuteReader(string.Format("{0}", where)))
            {
                while (sdr.Read())
                {
                    PAY_TYPE = GetData(PAY_TYPE, sdr);
                    list.Add(PAY_TYPE);
                }
            }
            sdr.Close();
            return list;
        }
        public bool Insert(ref PAY_TYPE PAY_TYPE)
        {
            try
            {
                Current.Application.Lock();
                MySqlParameter[] p = new MySqlParameter[]{
                new MySqlParameter("@title",PAY_TYPE.title),
                new MySqlParameter("@name",PAY_TYPE.name),
                new MySqlParameter("@mch_id",PAY_TYPE.mch_id),
                new MySqlParameter("@app_secret",PAY_TYPE.app_secret),
                new MySqlParameter("@app_id",PAY_TYPE.app_id),
                new MySqlParameter("@api_key",PAY_TYPE.api_key),
                new MySqlParameter("@wx_type",PAY_TYPE.wx_type),
                new MySqlParameter("@private_key",PAY_TYPE.private_key),
                new MySqlParameter("@public_key",PAY_TYPE.public_key),
                new MySqlParameter("@web_url",PAY_TYPE.web_url),
                new MySqlParameter("@file",PAY_TYPE.file)
            };

                int i = Convert.ToInt32(MYSQL.ExecuteSql(string.Format("insert into PAY_TYPE values(null,@title,@name,@mch_id,@app_secret,@app_id,@api_key,@wx_type,@private_key,@public_key,@web_url,@file)"), p));
                return i == 0 ? false : true;
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                Current.Application.UnLock();
            }
        }
        public bool Update(PAY_TYPE PAY_TYPE)
        {
            MySqlParameter[] p = new MySqlParameter[]{
                new MySqlParameter("@ID",PAY_TYPE.ID),
                new MySqlParameter("@title",PAY_TYPE.title),
                new MySqlParameter("@name",PAY_TYPE.name),
                new MySqlParameter("@mch_id",PAY_TYPE.mch_id),
                new MySqlParameter("@app_secret",PAY_TYPE.app_secret),
                new MySqlParameter("@app_id",PAY_TYPE.app_id),
                new MySqlParameter("@api_key",PAY_TYPE.api_key),
                new MySqlParameter("@wx_type",PAY_TYPE.wx_type),
                new MySqlParameter("@private_key",PAY_TYPE.private_key),
                new MySqlParameter("@public_key",PAY_TYPE.public_key),
                new MySqlParameter("@web_url",PAY_TYPE.web_url),
                new MySqlParameter("@file",PAY_TYPE.file) 
            };
            int i = MYSQL.ExecuteSql(string.Format("update PAY_TYPE set title=@title,name=@name,mch_id=@mch_id,app_secret=@app_secret,app_id=@app_id,api_key=@api_key,wx_type=@wx_type,private_key=@private_key,public_key=@public_key,web_url=@web_url,file=@file where ID=@ID"),p);
            return i == 0 ? false : true;
        }
        public bool Delete(int ID)
        {
            int i = MYSQL.ExecuteSql(string.Format("delete from PAY_TYPE where ID={0}", ID));
            return i == 0 ? false : true;
        }
        private PAY_TYPE GetData(PAY_TYPE PAY_TYPE, MySqlDataReader sdr)
        {
            PAY_TYPE = new PAY_TYPE();
            PAY_TYPE.ID = (int)sdr["ID"];
            PAY_TYPE.title = sdr["title"].ToString();
            PAY_TYPE.name = sdr["name"].ToString();
            PAY_TYPE.mch_id = sdr["mch_id"].ToString();
            PAY_TYPE.app_secret = sdr["app_secret"].ToString();
            PAY_TYPE.app_id = sdr["app_id"].ToString();
            PAY_TYPE.api_key = sdr["api_key"].ToString();
            PAY_TYPE.wx_type = Convert.ToInt32(sdr["wx_type"]);
            PAY_TYPE.private_key = sdr["private_key"].ToString();
            PAY_TYPE.public_key = sdr["public_key"].ToString();
            PAY_TYPE.web_url = sdr["web_url"].ToString();
            PAY_TYPE.file = sdr["file"].ToString(); 
            return PAY_TYPE;
        }
        /// <summary>
        /// 将DataTable数据转换成实体类列表
        /// 郑科 
        /// 日期：2016年11月4日10.18
        /// </summary>
        /// <param name="dt"></param>
        /// <returns>List<School></returns>
        public List<PAY_TYPE> ParseToList(DataTable dt)
        {
            if (dt == null || dt.Rows.Count < 1)
            {
                return null;
            }
            List<PAY_TYPE> PAY_TYPE_list = new List<PAY_TYPE>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                PAY_TYPE PAY_TYPE = new PAY_TYPE();
                PAY_TYPE.ID = int.Parse(dt.Rows[i]["ID"].ToString());
                PAY_TYPE.title = dt.Rows[i]["title"].ToString();
                PAY_TYPE.name = dt.Rows[i]["name"].ToString();
                PAY_TYPE.mch_id = dt.Rows[i]["mch_id"].ToString();
                PAY_TYPE.app_secret = dt.Rows[i]["app_secret"].ToString();
                PAY_TYPE.app_id = dt.Rows[i]["app_id"].ToString();
                PAY_TYPE.api_key = dt.Rows[i]["api_key"].ToString();
                PAY_TYPE.wx_type = int.Parse(dt.Rows[i]["wx_type"].ToString());
                PAY_TYPE.private_key = dt.Rows[i]["private_key"].ToString();
                PAY_TYPE.public_key = dt.Rows[i]["public_key"].ToString();
                PAY_TYPE.web_url = dt.Rows[i]["web_url"].ToString();
                PAY_TYPE.file = dt.Rows[i]["file"].ToString();  
                PAY_TYPE_list.Add(PAY_TYPE);
            }
            return PAY_TYPE_list;
        }
        #endregion
        #region join
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="pageindex">第几页</param>
        /// <param name="pagesize">一页列表数量</param>
        /// <param name="recordcount">记录总数</param>
        /// <returns></returns>
        public List<PAY_TYPE> PagingQueryList(PageTable pageTable, out int recordcount)
        {
            recordcount = MYSQL.GetInt("select count(*) from PAY_TYPE where " + pageTable.Where);
            return ParseToList(MYSQL.GetDataTable(PageSql.GetPage(pageTable)));
        }
        #endregion
    }
}
